package com.example.tool.dao.util;

import com.example.tool.MyApplication;
import com.example.tool.dao.DaoManager;
import com.example.tool.dao.entity.ReXueCareerWeapon;
import com.example.tool.dao.entity.ReXueGameCareer;
import com.example.tool.greendao.ReXueCareerWeaponDao;
import com.example.tool.greendao.ReXueGameCareerDao;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;

/**
 * Created Date 2019/9/27
 *
 * @author liangkun
 * description：热血数据工具类
 */
public class RexueDaoUtil {
    private static RexueDaoUtil rexueDaoUtil;

    /**
     * @param
     * @return
     * @description
     * @author liangkun
     * @time 2019/9/27 14:33
     */
    public static RexueDaoUtil getRexueDaoUtil() {
        if (null == rexueDaoUtil) {
            synchronized (RexueDaoUtil.class) {
                if (null == rexueDaoUtil) {
                    rexueDaoUtil = new RexueDaoUtil();
                }
            }
        }
        return rexueDaoUtil;
    }


    /**
     * @return ReXueGameCareerDao
     * @description 获取热血职业
     * @author liangkun
     * @time 2019/9/27 14:34
     */
    public ReXueGameCareerDao getGameCareerDao() {
        return DaoManager.getInstance(MyApplication.getContext()).getDaoSession().getReXueGameCareerDao();
    }

    /**
     * @return ReXueGameCareerDao
     * @description 获取热血武器
     * @author liangkun
     * @time 2019/9/27 14:34
     */
    public ReXueCareerWeaponDao getReXueCareerWeaponDao() {
        return DaoManager.getInstance(MyApplication.getContext()).getDaoSession().getReXueCareerWeaponDao();
    }

    /**
     * @param
     * @return
     * @description 插入职业 ctm快捷键
     * @author liangkun
     * @time 2019/9/27 14:38
     */
    public void insertCareer(String career) {
        if (!hasCareer(career)) {
            ReXueGameCareer reXueGameCareer = new ReXueGameCareer();
            reXueGameCareer.setUuid(career);
            getGameCareerDao().insert(reXueGameCareer);
        }

    }

    /**
     * @param
     * @return
     * @description 插入武器名称
     * @author liangkun
     * @time 2019/9/27 14:40
     */
    public void insertWeapon(String idName, String parent) {
        if (!hasWeapon(idName)) {
            ReXueCareerWeapon weapon = new ReXueCareerWeapon();
            weapon.setCareerUuid(parent);
            weapon.setWeaponId(idName);
            getReXueCareerWeaponDao().insert(weapon);
        }

    }

    /**
     * @param career 职业
     * @return
     * @description 查询是否有
     * @author liangkun
     * @time 2019/9/27 15:30
     */
    public boolean hasCareer(String career) {
        QueryBuilder<ReXueGameCareer> builder = getGameCareerDao().queryBuilder();
        WhereCondition condition = ReXueGameCareerDao.Properties.Uuid.eq(career);
        ReXueGameCareer unique = builder.where(condition).unique();
        return unique != null;
    }

    /**
     * @param weapon 武器
     * @return
     * @description 查询是否有该武器
     * @author liangkun
     * @time 2019/9/27 15:30
     */
    public boolean hasWeapon(String weapon) {
        QueryBuilder<ReXueCareerWeapon> builder = getReXueCareerWeaponDao().queryBuilder();
        WhereCondition condition = ReXueCareerWeaponDao.Properties.WeaponId.eq(weapon);
        ReXueCareerWeapon unique = builder.where(condition).unique();
        return unique != null;
    }

    /**
     * @param
     * @return
     * @description 返回所有的职业
     * @author liangkun
     * @time 2019/9/27 16:51
     */
    public List<ReXueGameCareer> getCareerList() {
        QueryBuilder<ReXueGameCareer> builder = getGameCareerDao().queryBuilder();
        return builder.list();
    }

    /**
     * @param
     * @return
     * @description 返回所有的职业
     * @author liangkun
     * @time 2019/9/27 16:51
     */
    public List<String> getWeaponList(String uuid) {
        QueryBuilder<ReXueCareerWeapon> builder = getReXueCareerWeaponDao().queryBuilder();
        WhereCondition condition = ReXueCareerWeaponDao.Properties.CareerUuid.eq(uuid);
        List<ReXueCareerWeapon> list = builder.where(condition).list();
        List<String> weapons = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                weapons.add(list.get(i).getWeaponId());
            }

        }
        return weapons;
    }

    /**
     * @param
     * @return
     * @description 查询对应的武器名
     * @author liangkun
     * @time 2019/9/29 10:51
     */
    public List<String> queryWeapon(String weapon) {
        QueryBuilder<ReXueCareerWeapon> builder = getReXueCareerWeaponDao().queryBuilder();
        WhereCondition condition = ReXueCareerWeaponDao.Properties.WeaponId.eq(weapon);
        List<ReXueCareerWeapon> list = builder.where(condition).list();
        List<String> weapons = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                weapons.add(list.get(i).getWeaponId());
            }

        }
        return weapons;
    }

    /**
     * @param
     * @return
     * @description 模糊查询
     * @author liangkun
     * @time 2019/9/29 10:51
     */
    public List<String> queryVagueWeapon(String weapon) {
        QueryBuilder<ReXueCareerWeapon> builder = getReXueCareerWeaponDao().queryBuilder();
        WhereCondition condition = ReXueCareerWeaponDao.Properties.WeaponId.like("%" + weapon + "%");
        List<ReXueCareerWeapon> list = builder.where(condition).list();
        List<String> weapons = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                weapons.add(list.get(i).getWeaponId());
            }

        }
        return weapons;
    }

}
